package com.cxdr.awsips.distribute.pool;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cxdr.awsips.common.logger.LogFactory;
import com.cxdr.awsips.distribute.bean.UnCompleteFile;
import com.cxdr.awsips.distribute.processor.DistributeProccessor;

/**
 * 未完成文件池，文件完成情况
 * 
 * @author pugang
 * 
 */
public final class UnCompletePool {
	private static Logger logger = LogFactory.makeNewLogger(UnCompletePool.class);
	private final Map<String, UnCompleteFile> pool = new HashMap<String, UnCompleteFile>();
	private static final UnCompletePool instance = new UnCompletePool();
	private String getKey(String sourceID,String fileName){
		return sourceID+"/"+fileName;
	}
	/**
	 * 单实例模式
	 */
	private UnCompletePool(){
	}
	public static UnCompletePool getInstance(){
		return instance;
	}
	//添加已经分发的目标信息
	public void add(String fileName,String sourceID, String targetID,String result) {
		String key = getKey(sourceID, fileName);
		UnCompleteFile uFile = pool.get(key);
		if(uFile==null){
			uFile = new UnCompleteFile(fileName,sourceID);
		}
//		logger.info("文件 "+fileName+" 在target id 为:"+targetID+"已经分发完成");
		uFile.addTarget(targetID,result);
		pool.put(key, uFile);
		if(uFile.isComplete()){
			logger.info("文件 "+fileName+" 分发完成");
			//向完成队列中添加已经分发完成的文件
			DataPool.CompleteQueue.add(uFile);
	//		pool.remove(fileName);
		}
	}

	public UnCompleteFile get(String fileName) {
		return pool.get(fileName);
	}
	/**
	 * 把未完成的信息写入文件或数据库中
	 */
	public void saveUnCompleteInfo(){
		
	}
}
